package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.entity.SysUser;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.IIndexService;
import com.itheima.ydd.service.ISysConfigService;
import com.itheima.ydd.service.ISysUserService;
import com.itheima.ydd.service.IYddUserService;
import com.itheima.ydd.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/home/index")
@CrossOrigin
@Slf4j
public class YddIndexController {
    @Autowired
    private IYddUserService yddUserService;

    @PostMapping("/login")
    public R login(@RequestBody YddUser yddUser) {
        log.info("adadada{}",yddUser);
//        String loginName = yddUser.getLoginName();
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getMobile, yddUser.getMobile());
        YddUser one = yddUserService.getOne(wrapper);

        if (one == null) {
            return R.error("账号不存在");
        }

        String password = yddUser.getPassword();

        if (one.getPassword().equals(generatePassword(password))) {
            //把用户id 加密进token里

            // 生成token信息
            Map retMap = new HashMap();
            Map tokenMap = new HashMap<>();

            tokenMap.put("id", one.getId());
            tokenMap.put("mobile", one.getMobile());

            String token = JwtUtils.getToken(tokenMap);
            retMap.put("token", token);
            return R.success("token", JwtUtils.getToken(tokenMap));
        }
        return R.error("密码错误");

    }

//    @GetMapping("/getConfig")
//    public R systemConfig() {
//        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(SysConfig::getTabValue, "system");
//        List<SysConfig> sysConfigs = sysConfigService.list(wrapper);
//
//        HashMap<String, String> map = new HashMap<>();
//        for (SysConfig sysConfig : sysConfigs) {
//            map.put(sysConfig.getAttrKey(), sysConfig.getAttrValue());
//        }
//        return new R(map, "", 0);
//    }
//
//    @PostMapping("/profile")
//    @CrossOrigin
//    public R profile(HttpServletRequest request) {
//        String token = request.getHeader("x-token");
//
//        Claims claims = JwtUtils.getClaims(token);
//        Long userId = Long.valueOf((String) claims.get("userId"));
//
//        log.info("用户id:{}",userId);
//        return sysUserService.profile(userId);
//    }
//
    /**
     * md5加密
     *
     * @param password 密码
     */
        private String generatePassword(String password) {
        return DigestUtils.md5DigestAsHex(password.getBytes());
    }
//
//    @GetMapping("/baseInfo")
//    public R baseInfo(HttpServletRequest request) {
//        String token = request.getHeader("x-token");
//        log.info(token);
//        Claims claims = JwtUtils.getClaims(token);
//        Long userId = Long.valueOf((String) claims.get("userId"));
//
//        log.info("用户id:{}", userId);
//        return indexService.baseInfo(userId);
//    }

}
